
import java.util.ArrayList;
import java.util.List;

// 定义Student类
class Student {
    private int id;
    private String name;

    // 构造方法
    public Student(int id, String name) {
        this.id = id;
        this.name = name;
    }

    // getter和setter方法
    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    // toString方法，方便打印Student对象
    @Override
    public String toString() {
        return "Student{" +
                "id=" + id +
                ", name='" + name + '\'' +
                '}';
    }
}

public class Demo010 {
    public static void main(String[] args) {
        // 创建ArrayList来存储Student对象
        List<Student> students = new ArrayList<>();

        // 录入3个学生对象
        students.add(new Student(1, "张三"));
        students.add(new Student(2, "李四"));
        students.add(new Student(3, "王五"));

        // 遍历打印所有学生
        System.out.println("所有学生：");
        for (Student student : students) {
            System.out.println(student);
        }

        // 修改张三的姓名为张三丰
        for (Student student : students) {
            if ("张三".equals(student.getName())) {
                student.setName("张三丰");
                break; // 找到后跳出循环
            }
        }

        // 再次打印所有学生，查看修改效果
        System.out.println("修改张三姓名后：");
        for (Student student : students) {
            System.out.println(student);
        }

        // 删除王五这个对象
        for (int i = 0; i < students.size(); i++) {
            if ("王五".equals(students.get(i).getName())) {
                students.remove(i);
                break; // 找到后跳出循环
            }
        }

        // 打印删除王五后的学生列表
        System.out.println("删除王五后：");
        for (Student student : students) {
            System.out.println(student);
        }
    }
}